﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace PascalConverter
{
    class FileManager
    {
        public FileManager() { }

        public string rootPath { get; set; }

        public IEnumerable<string> GetFiles()
        {
            return Directory.EnumerateFiles(rootPath, "*.*", SearchOption.AllDirectories).Where(f => f.EndsWith(".asp") || f.EndsWith(".aspx") || f.EndsWith(".cs") || f.EndsWith(".js"));
        }

        public IEnumerable<string> GetDirectories()
        {
            return Directory.EnumerateDirectories(rootPath, "*", SearchOption.AllDirectories);
        }

        public IEnumerable<string> GetServerPages()
        {
            return Directory.EnumerateFiles(rootPath, "*.*", SearchOption.AllDirectories).Where(f => f.EndsWith(".asp") || f.EndsWith(".aspx"));
        }

        public string[] ReadFile(string path)
        {
            return File.ReadAllLines(path, Encoding.UTF8);
        }

        public void WriteFile(string path, string[] contents)
        {
            File.WriteAllLines(path, contents, Encoding.UTF8);
        }

        public ArrayList GetAllFileNames()
        {
            ArrayList fileNames = new ArrayList();
            foreach (var file in GetServerPages())
            {
                fileNames.Add(GetFileName(file));
            }
            return fileNames;
        }

        public ArrayList GetAllDirectoryNames()
        {
            ArrayList directoryNames = new ArrayList();
            foreach (var directory in GetDirectories())
            {
                directoryNames.Add(GetDirectoryName(directory));
            }
            return directoryNames;
        }

        private string GetFileName(string path)
        {
            return path.Split('\\')[path.Split('\\').Length - 1].Split('.')[0];
        }

        private string GetDirectoryName(string path)
        {
            return path.Split('\\')[path.Split('\\').Length - 1];
        } 
    }
}
